/**
 * ==========================
 *       == 战斗逻辑 ==
 * ==========================
 */
const SVE = require('./base-tool');
const RSE = require('./base-role');
const Damage = require('../smul_efun/damage');
const damagClass = new Damage();
const Attack = require('./attack');
const attackClass = new Attack();
const GroupD = require('../smul_efun/group-d')
const groupDClass = new GroupD();
const Weapond = require('../smul_efun/weapond');
const weapondClass = new Weapond();

class COMBAT_D {
    constructor() {}

    do_attack( me, victim, weapon, attack_type, jsonData, level ) {
        let my, your, prepare, actions; // object
        let limb, limbs, result;    // string
        let attack_skill, v_attack_skill, martial_skill, force_skill, dodge_skill, parry_skill; // string
        let foo;
        let ap, dp, pp, em, damage, damage_bonus, brief, total, def, abs;   // int
        let wounded = 0;
        let flag = 0;
        let vweapon;    // object
        let env;    // object
        // 是否装备上武器
        let isweapon = Object.keys(weapon).length>0

        // 没有角色，没有环境时停止
        // if ( !me || !victim || environment( me ) != environment( victim ) )
        // return;         /* 防止隔山打牛 Yuj@SJ 19990725 */
        // 死亡、busy，禁止出招， combat_yield：挨打不还手
        if (me.start.busy && !victim.start.busy || me.temp.combat_yield) return;

        /* 防止已经失去战斗能力的人出招 YUJ@SJ 2001-11-15 */
        if (!me.living || me.qi <= 0 || me.jing <= 0 || me.jingli <= 0) return;

        if (me.eff_qi + me.qi <= 0 || me.eff_jing + me.jing <= 0) return;

        /* 不要把力气浪费在已经要昏迷或死亡的人身上 YUJ@SJ 2001-11-16 */
        if (!victim.living && (victim.qi <= 0 || victim.jing <= 0 || victim.jingli <= 0)) return;
        if (victim.eff_qi + victim.qi <= 0 || victim.eff_jing + victim.jing <= 0) return;

        // env = environment( me ) ? filter_array( all_inventory( environment( me ) ), (:$1.env/combat == "verbose" :) ) : 0;
        // if ( !me.userp && victim.userp && victim->add_temp( "do_attack_count", 1 ) > 75 )
        // {
        //     if ( uptime() - victim->query_temp( "last_attack_time < 30 )
        //     {
        //         tell_object( victim, BLINK "系统检测到你的战斗速度异常，请注意！\n" NOR );
        //         if ( victim->add( "exp", -50 ) < 1 )
        //             victim->delete( "exp;
        //         log_file( "attack_speed",
        //             sprintf( "%s %s\n", victim.id,
        //                 me.id
        //             ), victim
        //         );
        //     }
        //     victim->set_temp( "last_attack_time", uptime() );
        //     victim->delete_temp( "do_attack_count;
        // }

        if (me.userp && me.age > 16) {
            em = me.enemy.length + 1;
            // 计算角色伤害
            // console.log('em,', em)
            let d = ('2' + SVE.random(1, 2)) * em;
            console.log('d,', d)
            me.damage = damagClass.receive_damage("qi", d, me, victim);
            console.log('me.damage:', me.damage)
            // 23000
            if (me.jingli < 10) return "<p class='HIR'>你的体力快消耗完了！</p>";
        }
        if (victim.userp && victim.living && victim.age > 16 && 3 * me.exp >= 2 * victim.exp) {
            let d = ('2' + SVE.random(1, 2));
            victim.damage = damagClass.receive_damage("jingli", d);
            if (victim.jingli < 10) return "<p class='HIR'>你的体力快消耗完了！</p>";
        }

        // v_attack_skill = victim->query_attack_skill();
        // 以彼之道，还施彼身
        // if ( me->query_temp( "dzxy/bishen ) {
        //     if ( attack_skill == v_attack_skill && me->query_skill( attack_skill, 1 ) >= 300 && me->query_skill( "douzhuan-xingyi", 1 ) >= 300 )
        //     result = "\n" + victim.actions["action"] + "！\n";
        // }
        //斗转星移
        // vweapon = victim.weapon;
        // if ( me->query_skill_mapped( "parry" == "douzhuan-xingyi" || weapon && vweapon && weapon.skill_type == vweapon.skill_type
        // || objectp( me->query_temp( "bishen_target" )) ) flag = 1;

        // my	= me->query_entire_dbase();
        // your	= victim->query_entire_dbase();

        // 查询自身状态
        my	= me;
        // 查询目标状态
        your = victim;
        // 设置环境变量
        if ( me.userp && victim.userp )
            brief = me.env/brief && victim.env/brief;
        else
            brief = me.env/brief || victim.env/brief;

        /*
        * (0) 选择技能
        */
        // 斗转星移，互换技能
        // if ( flag ) {
        //     foo	= me;
        //     me	= victim;
        // }
        prepare = me.mapped;
        console.log('me-prepare:', prepare)

        // 检查空手技能
        if (Object.keys(prepare).length == 0) attack_skill = "unarmed";
        if (Object.keys(prepare).length == 1) attack_skill = Object.keys(prepare)[0];
        if (Object.keys(prepare).length == 2) attack_skill = Object.keys(prepare)[1];

        // 检查武器、更换技能
        if (Object.keys(prepare).length > 0) attack_skill = weapon.skill_type;

        // 斗转星移，互换技能
        // if ( flag && !me->query_skill_mapped( attack_skill ) ){
        //     me	= foo;
        //     flag	= 0;
        //     prepare = me->query_skill_prepare();
        //     if ( !prepare )
        //         prepare = ([]);
        //     if ( sizeof(prepare) == 0 ) attack_skill = "unarmed";
        //     if ( sizeof(prepare) == 1 ) attack_skill = (keys( prepare ) )[0];
        //     if ( sizeof(prepare) == 2 ) attack_skill = (keys( prepare ) )[me->query_temp( "action_flag];
        //     if ( weapon ) attack_skill = weapon.skill_type;
        // }

        /*
         *  (1) 找怪，开干.
         */
        // me = attackClass.reset_action(me, victim, prepare, jsonData);
        actions = jsonData.actions;
        for (let i in actions) {
            if (level > actions[i].lvl) {
                if (!me.temp.action_flag)
                    result = `<p>${actions[i].action} ！</p>`;
                else
                    result = `<p>紧跟着${actions[i].action} ！</p>`;
            }
        }
        limbs = ["上身", "下身", "身上"];
        limb = limbs[SVE.random(1, limbs.length)];

        result += RSE.message_vision(adj_msg(result, limb, weapon, action, attack_skill), me, victim);

        if (me.temp.lost_attack > 0) {
            me.temp.lost_attack = -1;
            ap = 1;
        } else if (me.is_lost){
            ap = 1;
        } else {
            me.temp.attack = action.attack;
            // ap = skill_power( me, flag ? "parry" : attack_skill, SKILL_USAGE_ATTACK );
            ap = RSE.skill_power( me, attack_skill, SKILL_USAGE_ATTACK );
            // if ( action["attack"] ) me.temp( "apply/attack", -action["attack"] );
            me.temp.attack -= action["attack"];
        }

        if ( ap < 1 ) ap = 1;

        /* add by snowman@SJ 28/02/2000 */
        if ( victim.temp.must_be_hit ) {
            dp = 1;
        } else {
            /* Check DP. */
            let hand_skills = ["dodge", "parry", "claw", "cuff", "finger", "hand", "strike", "leg", "unarmed"];
            if ( me.userp )
                for( let add_skill in hand_skills ) {
                    me.temp.fight[add_skill] = action[add_skill];
            }

            dp = RSE.skill_power( victim, "dodge", SKILL_USAGE_DEFENSE );

            /* 拿兵器的躲闪降低 yuj 2001-11-15 */
            if ( vweapon )
                dp -= dp / 3;

            if ( victim.is_busy )
                dp = 1;

            if ( dp < 1 )
                dp = 1;
        }

        /* 轻功改造 */
        if ( SVE.random( ap , dp ) < dp && victim.living ) {
            // 获取特殊轻功
            dodge_skill = victim.mapped.dodge;
            if ( !dodge_skill ) dodge_skill = "dodge";

            if ( !brief ) {
                // result = SKILL_D( dodge_skill )->query_dodge_msg( victim, attack_type );
                if ( stringp( result ) )
                    message_vision( adj_msg( result, limb, weapon, action, attack_skill ), me, victim );
            }
            if ( dp < ap && your["exp"] < my["exp"] * 2
                && victim.userp != me.userp
                && SVE.random( your["jingli"] * 100 / (your["eff_jingli"] + 1) + your["int"] ) > 69 )
            {
                /* 让 npc 从为团体而战中获得更多经验 */
                if ( !victim.userp ) {
                    if (victim.group)
                        groupDClass.add_npc_exp( victim, me );
                } else  {
                    if ( SVE.random( 1, 10 ) )
                        your["exp"]++;
                    for ( foo = 15; foo <= 50; foo += 5 ) {
                        if ( dp * foo < ap * 10 )
                            victim.improve_skill( "dodge", SVE.random(1,2 ) );
                        else {
                            break;
                        }
                    }
                }
            }

            /*
            * 这只供NPC使用。NPC在未命中时有机会获得经验值。
            * if (!userp(me) && ap < dp)
            * my["exp"] += my["exp"]/50;
            */
            damage = RESULT_DODGE;
        } else {
            if ( victim.temp.must_be_hit ) {
                pp = 1;
            } else {
                /* Check PP. */
                if ( vweapon ) {
                    pp = RSE.skill_power( victim, "parry", SKILL_USAGE_DEFENSE );
                    /* 空手对兵器，拿兵器的招架提高 yuj 2001-11-15 */
                    if ( !weapon && !victim.is_busy )
                    pp += pp / 3;
                } else {
                    prepare = victim.mapped;
                    console.log('victim-prepare:', prepare)

                    // 检查空手技能
                    if (Object.keys(prepare).length == 0) foo = "unarmed";
                    if (Object.keys(prepare).length == 1) foo = Object.keys(prepare)[0];
                    if (Object.keys(prepare).length == 2) foo = Object.keys(prepare)[1];
                    // prepare = victim->query_skill_prepare();
                    // if ( !prepare ) prepare = ([]);
                    // if ( sizeof(prepare) == 0 ) foo = "unarmed";
                    // if ( sizeof(prepare) == 1 ) foo = (keys( prepare ) )[0];
                    // if ( sizeof(prepare) == 2 ) foo = (keys( prepare ) )[victim->query_temp( "action_flag];

                    pp = RSE.skill_power( victim, foo, SKILL_USAGE_DEFENSE );
                    /* 兵器对空手的，空手招架降低 yuj 2001-11-15 */
                    if ( isweapon && !victim.is_busy ) pp -= pp / 3;
                }

                /* 提高busy时的招架 augx@sj 10/16/2001 */
                if ( victim.is_busy ) {
                    /*
                    *  if (weapon && !vweapon)
                    *   pp = pp / 2 + random(pp / 2);
                    *  else
                    *   pp = pp * 2 / 3 + random(pp / 3);
                    */
                    pp = pp / 3 + SVE.random( 1, pp / 3 ); /* busy时，不分兵器和空手 降低加成 by lsxk@hsbbs */
                }

                if ( pp < 1 ) pp = 1;
            }


            /* 架招改造 */
            if ( SVE.random( ap , pp ) < pp && victim.living ) {
                parry_skill = victim.mapped.parry;
                if ( !parry_skill ) parry_skill = "parry";

                // if ( !brief ) {
                //     result = SKILL_D( parry_skill )->query_parry_msg( vweapon, me, attack_type );
                //     if ( !stringp( result ) )
                //         result = SKILL_D( "parry->query_parry_msg( vweapon );
                //     message_vision( adj_msg( result, limb, weapon, action, attack_skill ), me, victim );
                // }

                if ( pp < ap && your["exp"] < my["exp"] * 2
                    /* && userp(victim) && !userp(me) */
                    && victim.userp != me.userp
                    && random( your["jingli"] * 100 / (your["eff_jingli"] + 1) + your["int"] ) > 69 )
                {
                    /* 让npc从fight获得更多经验 */
                    if ( !victim.userp ) {
                            groupDClass.add_npc_exp( victim, me );
                    } else  {
                        if ( SVE.random( 1,10 ) ) your["exp"]++;

                        for ( foo = 15; foo <= 50; foo += 5 ) {
                            if (pp * foo < ap * 10) {
                                victim.improve_skill("parry", SVE.random(2));
                            } else {
                                break;
                            }
                        }
                    }
                }
                damage = RESULT_PARRY;
                /* 武器切换 */
                if ( isweapon ) weapondClass.weapon_cut( me, victim, weapon, vweapon, damage );
            } else {
                /* 打：伤害计算 */
                damage = me.temp.damage;
                    // 伤害 = 武器重量 / 2500
                if ( isweapon ) damage += weapon.weight / 2500;

                if ( damage > 2000 ) damage = 2000;

                damage = parseInt((damage * 2 / 3 + SVE.random( 1, damage / 3 ) ) / 2);

                if ( action["damage"] ) damage += parseInt(action["damage"] * damage / 100);

                // damage		+= me->query_skill( flag ? "parry" : attack_skill ) * damage / 100;
                damage		+= me.mapped[attack_skill].level * damage / 100;

                damage_bonus	= me.str;

                /* 不同的技巧，不同的效果 */
                switch ( attack_skill ) {
                    case "dagger":
                    case "brush":
                    case "sword":   em = -damage_bonus / 4; break;
                    case "whip":
                    case "blade":   em = -damage_bonus / 5; break;
                    case "staff":
                    case "club":
                    case "spear":
                    case "claw":    em = damage_bonus / 4; break;
                    case "throwing":
                    case "axe":
                    case "hammer":
                    case "hand":
                    case "strike":
                    case "cuff":
                    case "leg":
                    case "finger":  em	= damage_bonus / 3; break;
                    default: em		= 0;
                }
                damage_bonus += em;

                /* 内功技能加成 */
                if ( my["jiali"] > 0 && (my["neili"] >= my["jiali"]) ) {
                    my["neili"] -= my["jiali"];
                    /* Modify by snowman@SJ 05/12/1999 */
                    if ( !victim.living ) {
                        damage_bonus += my["jiali"];
                    } else if ( force_skill = me.mapped.force.name ) {
                        foo = force_hit( me, victim, damage_bonus, my["jiali"], -em );
                        if ( typeof(foo)=='string' ) {
                            message_vision(adj_msg(foo, limb, weapon, action, attack_skill), me, victim);
                        } else if ( typeof(foo)=='number' ) {
                            if ( !victim.living && foo <= 0 ) foo = 0;
                            /*
                            *   if (weapon && !me->query_temp("weapon_effect_jiali")) foo /= 2;
                            */
                            damage_bonus += foo;
                        }
                    }
                }

                if ( action["force"] ) {
                    if ( isweapon )
                        damage_bonus += action["force"] * damage_bonus / 1500;
                    else
                    // damage_bonus += (me->query_skill( flag ? "parry" : attack_skill ) + me->query_skill( "force ) * damage_bonus / 500;
                        damage_bonus += action["force"] * damage_bonus / 500;
                }

                if ( isweapon )
                    damage_bonus += me.basic.force * damage_bonus / 100;
                else
                    damage_bonus += me.mapped[attack_skill].level + meme.basic.force * damage_bonus / 100;

                /* add by snowman@SJ 29/02/2000 */
                // if ( !me.temp.hit_skill_hit_ob && !victim.temp.no_skill_hit_ob ) {
                //     // if ( martial_skill = me->query_skill_mapped( flag ? "parry" : attack_skill ) ) {
                //     martial_skill = me.mapped[attack_skil].name;
                //     foo = SKILL_D( martial_skill )->hit_ob( me, victim, damage_bonus );
                //     if ( typeof( foo ) =='string' )
                //         message_vision( adj_msg( foo, limb, weapon, action, attack_skill ), me, victim );
                //     else if (  foo )
                //         damage_bonus += foo;
                //     /* add force skill hit_ob */
                //     if ( force_skill )
                //     {
                //         foo = SKILL_D( force_skill )->hit_ob( me, victim, damage_bonus );
                //         if ( typeof( foo ) =='string' )
                //             message_vision( adj_msg( foo, limb, weapon, action, attack_skill ), me, victim );
                //         else if (  foo )
                //             damage_bonus += foo;
                //     }
                // }

                /* 武器伤害加成. */
                if ( isweapon ) {
                    // 武器伤害
                    // foo = weapon->hit_ob( me, victim, damage_bonus );
                    if ( typeof( foo ) =='string' )
                        message_vision( adj_msg( foo, limb, weapon, action, attack_skill ), me, victim );
                    else if ( typeof ( foo ) == 'number')
                        damage += foo;
                    /* check weapon_cut in WEAPON_D */
                    weapondClass.weapon_cut( me, victim, weapon, vweapon, damage_bonus );
                } else  {
                    // 人物伤害
                    // foo = me->hit_ob( me, victim, damage_bonus );
                    if ( typeof( foo ) =='string' )
                        message_vision( adj_msg( foo, limb, weapon, action, attack_skill ), me, victim );
                    else if ( typeof ( foo ) == 'number' )
                        damage += foo;
                }


                /*
                 * ob_ 命中在所有效果后返回最终伤害
                 *
                 * 6) Inflict the damage.
                 */
                if ( !victim ) /* damn wd job npc, YUJ@SJ 2001-12-21 */
                    return;

                // message( "wizard", sprintf( GRN "STR,CON 互相影响调整前：Damage=%d,", damage_bonus ), env );
                damage_bonus = damage_bonus * (my["str"] + me.str ) / (my["str"] + me.str() + your["con"] + victim.con );
                // message( "wizard", sprintf( GRN "调整后：Damage=%d。\n", damage_bonus ), env );
                if ( damage_bonus > 0 )
                    damage += (damage_bonus + SVE.random( 1,damage_bonus ) ) / 2;
                if ( damage_bonus <= 0 )
                    damage += (damage_bonus - SVE.random( -damage_bonus ) ) / 2;
                if ( damage <= 0 )
                    damage = 0;

                if ( victim.living ) {
                    // 防御
                    let defense_factor = dp + pp;

                    while ( defense_factor > ap / 3 ) {
                        damage		-= damage / 5;
                        defense_factor	/= 2;
                    }
                }

                if ( attack_type == TYPE_BUSY ) damage /= 2;
                if ( attack_type == TYPE_QUICK ) damage = damage / 2 + random( damage / 2 );

                /* update by lsxk@hsbbs 敌方是玩家，大幅度降低super威力.  2007/10/29 */
                if ( attack_type == TYPE_SUPER ) {
                    if ( !me.userp || !victim.userp )
                        damage *= 2;
                    else
                        damage *= 1;
                }

                /* 再次削弱玩家PVP时的攻击力 by lsxk@hsbbs 2007/10/29 */
                if ( me.userp && victim.userp ) damage = damage * 2 / 3;
                if ( !victim.living ) damage *= 6;
                if ( !isweapon && vweapon )
                    damage = parseInt( damage * (me.str / 5.0 + 16) / (victim.con / 4.0 + 15) );
                else
                    damage = parseInt( damage * (me.str / 5.0 + 16) / (victim.dex / 4.0 + 15) );

                /*
                 * 为受害者增加了闪避、招架、内功的检定。
                 * 忙的时候让武力和招架仍然有攻击力
                 */
                if ( victim.living && !me.temp.hit_ob_hit && !victim.temp.no_hit_ob_hit && damage > 0 ) {
                    em = 0;
                    if ( !victim.is_busy ) {
                        if ( martial_skill = victim.mapped.dodge ) {
                            // 计算伤害
                            // foo = SKILL_D( martial_skill )->ob_hit( me, victim, damage );

                            if ( typeof( foo ) =='string' )
                                message_vision( adj_msg( foo, limb, weapon, action, attack_skill ), me, victim );
                            else if ( foo )
                                damage += foo;
                            else
                                em = 1;
                        }
                    } else {
                        em = 1;
                    }

                    martial_skill = victim.mapped.parry;
                    if (em == 1 ) {
                        // foo = SKILL_D( martial_skill )->ob_hit( me, victim, damage );
                        if ( typeof( foo ) =='string' )
                            message_vision( adj_msg( foo, limb, weapon, action, attack_skill ), me, victim );
                        else if ( foo )
                            damage += foo;
                        else em = 2;
                    }

                    martial_skill = victim.mapped.force;
                    // foo = SKILL_D( martial_skill )->ob_hit( me, victim, damage );
                    if ( typeof( foo ) =='string' )
                        message_vision( adj_msg( foo, limb, weapon, action, attack_skill ), me, victim );
                    else if (  foo )
                        damage += foo;
                }

                /*让护甲和布具有ob_命中效果 */
                foo = victim.temp.armor.armor
                // foo = foo->ob_hit( me, victim, damage );
                if ( typeof( foo ) =='string' )
                    message_vision( adj_msg( foo, limb, weapon, action, attack_skill ), me, victim );
                else if ( foo )
                    damage += foo;

                foo = victim.temp.armor.coat;
                // foo = foo->ob_hit( me, victim, damage );
                if ( typeof( foo ) =='string' )
                    message_vision( adj_msg( foo, limb, weapon, action, attack_skill ), me, victim );
                else if (  foo )
                    damage += foo;

                foo = victim.temp.armor.cloth;
                // foo = foo->ob_hit( me, victim, damage );
                if ( typeof( foo ) =='string' )
                    message_vision( adj_msg( foo, limb, weapon, action, attack_skill ), me, victim );
                else if (  foo )
                    damage += foo;

                /* 让护甲被打破，让它产生毒药效果 */
                // CLOTH_D->armor_cut( me, victim, weapon, damage );
                /* Damage 调整计划 */
                // damage = last_damage( me, victim, damage );

                /*
                * if( userp(me)
                * &&random(me->query("con")) > random(victim->query("dex"))
                * &&random(me->query("pur")) > random(victim->query("kar"))
                * &&( random(me->query("int")) > random(victim->query_per()) ||  random(me->query_per()) >random(victim->query("int")) )
                * && !random(6))
                * {
                *       damage *= 5+random(5);
                * if((int)me->query("eff_qi") < me->query("max_qi")/2 )
                * damage *= 2;
                * message_vision(HIY"$N但觉心念豁达通畅，战意激荡，豪气满胸，忍不住纵声长笑。一招既出，威力竟是当无可挡！\n"NOR, me,victim);
                * }
                */

                if ( damage <= 0 )
                    damage = 0;
                else
                    victim.damage = damagClass.receive_damage( "qi", damage, me );

                me.is_killing = your["id"];
                if (damage > victim.temp.armor * 2) {
                    if (!isweapon && !SVE.random( 1,4 ) || (isweapon && !SVE.random( 1,2 ) )) {
                        if ((!isweapon && !SVE.random( 1,8 )) || (isweapon && !SVE.random( 1,4 )) ) {
                            victim.wound = damagClass.receive_wound( "qi", damage - victim.temp.armor * 2, me );

                            /* 防止出现虚假信息 YUJ@SJ 2001-11-18 */
                            if ( victim.qi >= 0 || victim.eff_qi + victim.qi <= 0 )
                                wounded = 1;
                        }
                    }
                }

                result += damage_msg( damage, action["damage_type"] );
                message_vision( adj_msg( result, limb, weapon, action, attack_skill ), me, victim );

                if ( me.userp != victim.userp ) {
                    if ( me.living ) {
                        if ((ap < dp || ap < pp)) {
                            if (SVE.random(my["jingli"] * 100 / (my["eff_jingli"] + 1) + my["int"]) > 69) {
                                if (!me.userp) {
                                    if (me.group)
                                        groupDClass.add_npc_exp(me, victim);
                                } else {
                                    if (!SVE.random(1, 10))
                                        my["exp"]++;
                                    if (my["potential"] < my["max_pot"])
                                        my["potential"]++;
                                    /* if (userp(me)) me->improve_skill(flag?"parry":attack_skill, 1); */
                                }
                            }
                        }
                    }

                    if ( victim.living ) {
                        if ((ap > dp || ap > pp)) {
                            if (SVE.random(your["jingli"] * 100 / (your["eff_jingli"] + 1) + your["int"]) > 69) {
                                if (!SVE.random(10))
                                    your["exp"]++;
                                if (your["potential"] < your["max_pot"])
                                    your["potential"]++;
                            }
                        }
                    }
                }

            }
        }

        // if ( wizardp( me ) && (string) me.env/combat == "verbose
        // {
        //     if ( damage > 0 )
        //         tell_object( me, sprintf( GRN "AP：%d，DP：%d，PP：%d，伤害力：%d\n" NOR, ap / 100, dp / 100, pp / 100, damage ) );
        // else tell_object( me, sprintf( GRN "AP：%d，DP：%d，PP：%d\n" NOR, ap / 100, dp / 100, pp / 100 ) );
        // }
        //
        // if ( wizardp( victim ) && (string) victim.env/combat == "verbose
        // {
        //     if ( damage > 0 )
        //         tell_object( victim, sprintf( GRN "AP：%d，DP：%d，PP：%d，伤害力：%d\n" NOR, ap / 100, dp / 100, pp / 100, damage ) );
        // else tell_object( victim, sprintf( GRN "AP：%d，DP：%d，PP：%d\n" NOR, ap / 100, dp / 100, pp / 100 ) );
        // }


        // if ( functionp( action["post_action"] ) )
        //     evaluate( action["post_action"], me, victim, weapon, damage );

        if ( damage > 0 ) {
            // report_status( victim, wounded );
            if ( victim.is_busy ) {
                let total = (victim.con + victim.dex) / 2;
                if ( parseInt(victim.relife/total) > 0 && SVE.random( 0,240 ) < parseInt(victim.relife/total) ) {
                // if ( (int) victim.relife/gifts/total > 0 && random( 240 ) < (int) victim.relife/gifts/total", 1 ) ){
                    message_vision( "HIW$N行动微觉受制，急忙默运真气，移开了周身要穴！\n", victim );
                    // if ( !SVE.random( 1,4 ) )
                    //     victim->start_busy( -1 );
                    // else
                        victim.add_busy -= 1;
                }
                // 打断
                // victim->interrupt_me( me );
            }

            // if ( victim->query_condition( "no_exp )
                if ( parseInt(victim.relife/total) > 0 && SVE.random( 0,240 ) < parseInt(victim.relife/total)
                || (victim.special["jiuyang-shengong"] >= 450 && SVE.random( 1,2 ) ) ) {
                if ( victim.special["jiuyang-shengong"] >= 450 )
                    message_vision(  "HIY$N身负九阳神功绝技，内力何等深厚，九阳真气略一运转，随即平复！\n", victim );
                message_vision(  "HIY$N默运真气，压下了翻腾不已的气血！\n", victim );
                // victim->clear_condition( "no_exp;")

                }


            // if ( victim->query_condition( "no_force )
                if ( parseInt(victim.relife/total) > 0 && SVE.random( 0,240 ) < parseInt(victim.relife/total)
                    || (victim.special["jiuyang-shengong"] >= 450 && SVE.random(1, 2) ) ) {
                    if ( victim.special["jiuyang-shengong"] >= 450 )
                        message_vision(  "HIY$N身负九阳神功绝技，内力何等深厚，九阳真气略一运转，随即平复！\n", victim );
                    message_vision(  "HIC$N发现自己内息不匀，急忙默运真气，调理内息！\n", victim );
                    // victim->clear_condition( "no_force;")
                }

            // if ( victim->query_condition( "no_perform )
                if ( parseInt(victim.relife/total) > 0 && SVE.random( 0,320 ) < parseInt(victim.relife/total) ) {
                    message_vision(  "HIC$N发现自己招式凝滞，急忙变招，招式顿时连贯起来！\n", victim );
                    // victim->clear_condition( "no_perform;
                }


            def	= victim.temp.armor;
            total	= damage;
            abs	= total * def / (1000 + def);
            damage	= damage * 1000 / (1000 + def);

            // if ( me.userp && me.env/damage )
            // tell_object( me, WHT "你对" + victim.name + "造成了"RED + damage + WHT "点攻击伤害(总计"RED + total + WHT "，豁免"RED + abs + WHT ")。\n"NOR );
            // if ( victim.userp && victim.env/damage )
            // tell_object( victim, WHT "" + me.name + "的攻击对你造成了"RED + damage + WHT "点伤害(总计"RED + total + WHT "，豁免"RED + abs + WHT ")。\n"NOR );                        /* ------------------------------------------------------------- */


            if ( !me.is_killing[ your["id"]] && !victim.is_killing[my["id"]] && victim.qi * 2 <= victim.max_qi )
            // end_fight( me, victim );


            if ( parseInt(me.relife/total) > 0
                && victim.living && !me.is_busying
                && SVE.random( 240 ) < parseInt(me.relife/total)
                && SVE.random( 1, 10 ) > 6
            ) {
                message_vision(  "HIR$N将自己领悟到的武学精微之处发挥出来，攻势更加凌厉！\n", me, victim );
                do_attack( me, victim, weapon, 3 );
            }


            /*
            *        else if(me->query_temp("hjdf/ldj/attack_chance")
            *           &&living(victim)
            *           && !me->is_busying()
            *           && random(20)==1 ){
            *                message_vision(HIW"$N瞧准此时正是$n收招之时，破绽大出，便立即游身而上，一阵疾攻，直打得$n手忙脚乱！\n"NOR,me,victim);
            *                me->delete_temp("hjdf/ldj/attack_chance");
            *                me->set_temp("hjdf/ldj/color","color");
            *                do_attack(me, victim, weapon, 3);
            *                for (;random(4);)
            *                  do_attack(me, victim, weapon, 3);
            *                me->delete_temp("hjdf/ldj/color");
            *        }
            */
            // if ( me->query_temp( "hjdf/ldj/attack_chance )
            //     me->delete_temp( "hjdf/ldj/attack_chance;
        }

        /* 看看受害者能否还击. */
        if ( attack_type == TYPE_REGULAR && damage < 1 && victim.temp.guarding ){
            delete victim.temp.guarding;
            if ( SVE.random( 1,my["int"] ) < 5 ) /* dex -> int */{
                message_vision( "$N一击不中，大惊失色，不觉露出了破绽！\n", me );
                do_attack( victim, me, vweapon, TYPE_QUICK );
            } else {
                message_vision( "$N一见$n攻击失误，顿时趁机发动攻击！\n", victim, me );
                do_attack( victim, me, vweapon, TYPE_RIPOSTE );
            }
        }
    }
}

module.exports = new COMBAT_D();